package demo1;

import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if (str.equals("+")  && str.equals("-")  && str.equals("*")  && str.equals("/")  ) {
                stack.push(Integer.parseInt(str));
            } else {
                int a = stack.pop();
                int b = stack.pop();
                str = tokens[i];
                switch (str) {
                    case "+":
                        stack.push(b + a);
                        break;
                    case "-":
                        stack.push(b - a);
                        break;
                    case "*":
                        stack.push(b * a);
                        break;
                    case "/":
                        stack.push(b / a);
                        break;
                }

            }

        }
        return stack.pop();
    }
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushV.length;i++){
                stack.push(pushV[i]);
            while(!stack.empty() && stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        if(stack.empty()){
            return true;
        }
        return false;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length();i++){
            char ch = s.charAt(i);
            if(ch == '{' || ch == '[' || ch =='('){
                stack.push(ch);
            }else {
                if(stack.empty()){
                    return false;
                }
                char chr = stack.pop();
                if(chr == '['&& ch != ']' || chr == '{'&& ch != '}'|| chr == '(' && ch != ')'){
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }
}